package hu.veilingsite.server;

import hu.veilingsite.shared.ClientUser;

import java.io.Serializable;
import java.sql.Date;

@SuppressWarnings("serial")
public class User implements Serializable {
	private int id,credits = 0;
	private String name,address,residence,password = "-1",email;
	private Date birthdate;
	private boolean maybid,mayoffer,isadmin;
	
	public User(int id,String nm,String ad,String rs,Date bd,int mb,int mo,int ia,int cr,String pw,String em) {
		this.id = id;
		name = nm;
		address = ad;
		residence = rs;
		birthdate = bd;
		if (mb > 0) {
			maybid = true;
		} else { maybid = false; }
		if (mo > 0) {
			mayoffer = true;
		} else { mayoffer = false; }
		if (ia > 0) {
			isadmin = true;
		} else { isadmin = false; }
		credits = cr;
		password = pw;
		email = em;
	}
	
	public User(String nm,String ad,String rs,Date bd,boolean mb,boolean mo,boolean ia,int cr,String pw,String em) {
		name = nm;
		address = ad;
		residence = rs;
		birthdate = bd;
		maybid = mb;
		mayoffer = mo;
		isadmin = ia;
		credits = cr;
		password = pw;
		email = em;
	}
	
	public User() {
	}

	public boolean withdrawCredits(int cr) {
		if (cr > credits) {
			return false;
		} else {
			credits -= cr;
			return true;
		}
	}
	
	public void depostCredits(int cr) {
		credits += cr;
	}
	
	public boolean checkPassword(String pw) {
		if (password.equals(pw)) {
			return true;
		} else {
			return false;
		}
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getResidence() {
		return residence;
	}
	public void setResidence(String residence) {
		this.residence = residence;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}
	public boolean isMaybid() {
		return maybid;
	}
	public void setMaybid(boolean maybid) {
		this.maybid = maybid;
	}
	public boolean isMayoffer() {
		return mayoffer;
	}
	public void setMayoffer(boolean mayoffer) {
		this.mayoffer = mayoffer;
	}
	public boolean isIsadmin() {
		return isadmin;
	}
	public void setIsadmin(boolean isadmin) {
		this.isadmin = isadmin;
	}
	public int getCredits() {
		return credits;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return password;
	}

	public java.sql.Date getBirthDate() {
		return birthdate;
	}

	public int getMaybid() {
		if (maybid) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public int getMayoffer() {
		if (mayoffer) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public int getIsadmin() {
		if (isadmin) {
			return 1;
		} else {
			return 0;
		}
	}

	public void fromClientUser(ClientUser user) {
		id = user.getId();
		credits = user.getCredits();
		name = user.getName();
		address = user.getAddress();
		residence = user.getResidence();
		email = user.getEmail();
		birthdate = user.getBirthdate();
		maybid = user.isMaybid();
		mayoffer = user.isMayoffer();
		isadmin = user.isIsadmin();
		System.out.println("user created");
	}
	
	public ClientUser makeClient(){
		return new ClientUser(id,name, address, residence, birthdate, maybid, mayoffer, isadmin, credits, email);
	}

	@Override
	public String toString() {
		return "User [address=" + address + ", birthdate=" + birthdate
				+ ", credits=" + credits + ", email=" + email + ", id=" + id
				+ ", isadmin=" + isadmin + ", maybid=" + maybid + ", mayoffer="
				+ mayoffer + ", name=" + name + ", password=" + password
				+ ", residence=" + residence + "]";
	}
}